import { lazyBatchReport } from '../report/index'
import { generateUniqueId } from '../utils/index'

export default function pageChange() {
  let oldUrl = ''

  window.addEventListener(
    'hashchange',
    (evant) => {
      const newUrl = evant.newURL
      const reportData = {
        from: oldUrl,
        to: newUrl,
        uuid: generateUniqueId(),
        type: 'behavior',
        subType: 'hashchange',
        startTime: performance.now(),
      }
      // 数据上报
      lazyBatchReport(reportData)
      oldUrl = window.location.href
    },
    true,
  )

  let from = ''
  window.addEventListener(
    'popstate',
    () => {
      const to = window.location.href
      const reportData = {
        from,
        to,
        uuid: generateUniqueId(),
        type: 'behavior',
        subType: 'popstate',
        startTime: performance.now(),
      }
      // 数据上报
      lazyBatchReport(reportData)
      from = to
    },
    true,
  )
}
